package com.tiger.cloud.controller.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.system.SysScheduleJob;
import com.tiger.cloud.service.system.ISysScheduleJobService;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;

@Controller
@RequestMapping(value = "scheduleJob")
@Api(hidden=true)
public class SysScheduleJobController extends BaseController {

	@Autowired
	private ISysScheduleJobService sysScheduleJobService;
	
	@IgnoreAuth
	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request) {
		return "/system/scheduleJob/scheduleJob-list";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "insertGet", method = RequestMethod.GET)
	public String insertGet(HttpServletRequest request) {
		return "/system/scheduleJob/scheduleJob-insert";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "updateGet", method = RequestMethod.GET)
	public String update(HttpServletRequest request,String id) {
		request.setAttribute("sysScheduleJob", sysScheduleJobService.get(id));
		return "/system/scheduleJob/scheduleJob-update";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "info", method = RequestMethod.GET)
	public String info(HttpServletRequest request,String id) {
		request.setAttribute("sysScheduleJob", sysScheduleJobService.get(id));
		return "/system/scheduleJob/scheduleJob-info";
	}
	
	@IgnoreAuth
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,HttpServletRequest request) throws SchedulerException{
		Map<String, Object> params=getPageMap(request);
		List<SysScheduleJob> scheduleJobs=sysScheduleJobService.getAllJob();
		List<SysScheduleJob> datas=sysScheduleJobService.find(params);
		for (SysScheduleJob sysScheduleJob : datas) {
			for (SysScheduleJob scj : scheduleJobs) {
				if(sysScheduleJob.getJobGroup().equals(scj.getJobGroup()) && sysScheduleJob.getJobName().equals(scj.getJobName())){
					sysScheduleJob.setJobStatus(scj.getJobStatus());
				}
			}
		}
		return ResponseUtil.ok(datas);
	}
	
	@IgnoreAuth
	@RequestMapping(value = "stop", method = RequestMethod.POST)
	@ResponseBody
	public Response stop(HttpServletRequest request,String id) throws SchedulerException{
		SysScheduleJob scheduleJob=sysScheduleJobService.get(id);
		sysScheduleJobService.pauseJob(scheduleJob);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "startNow", method = RequestMethod.POST)
	@ResponseBody
	public Response startNow(HttpServletRequest request,String id) throws SchedulerException{
		SysScheduleJob scheduleJob=sysScheduleJobService.get(id);
		sysScheduleJobService.runAJobNow(scheduleJob);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "resume", method = RequestMethod.POST)
	@ResponseBody
	public Response resume(HttpServletRequest request,String id) throws SchedulerException{
		SysScheduleJob scheduleJob=sysScheduleJobService.get(id);
		sysScheduleJobService.resumeJob(scheduleJob);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(HttpServletRequest request,String cron,String id) throws SchedulerException{
		sysScheduleJobService.updateCron(id, cron);
		return ResponseUtil.ok();
	}
	
	@IgnoreAuth
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(HttpServletRequest request,SysScheduleJob sysScheduleJob) throws SchedulerException{
		sysScheduleJob.setJobStatus(SysScheduleJob.STATUS_RUNNING);
		sysScheduleJob.setIsConcurrent(SysScheduleJob.CONCURRENT_IS);
		sysScheduleJob.setJobType("1");
		sysScheduleJobService.addJob(sysScheduleJob);
		return ResponseUtil.ok();
	}

	@IgnoreAuth
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(String id,HttpServletRequest request) throws SchedulerException{
 		SysScheduleJob scheduleJob=sysScheduleJobService.get(id);
		sysScheduleJobService.deleteJob(scheduleJob);
		return ResponseUtil.ok();
	}

}